package controller

import (
	"context"
	v1 "gf-admin-api-template/api/v1"
	"gf-admin-api-template/internal/model"
	"gf-admin-api-template/internal/service"
)

type cRole struct{}

var Role = cRole{}

func (r *cRole) List(ctx context.Context, req *v1.RoleListReq) (res *v1.RoleListRes, err error) {
	total, list, err := service.Role().List(ctx, model.RoleGetListInput{
		RoleCreateInput: model.RoleCreateInput{
			Name: req.Name,
			Slug: req.Slug,
		},
		Page:     req.Page,
		PageSize: req.PageSize,
	})
	if err == nil {
		res = &v1.RoleListRes{
			Total: total,
			List:  list,
		}
	}
	return
}

func (r *cRole) Create(ctx context.Context, req *v1.RoleCreateReq) (res *v1.RoleCreateRes, err error) {
	err = service.Role().Create(ctx, model.RoleCreateInput{
		Name: req.Name,
		Slug: req.Slug,
	}, req.Menus)
	return
}

func (r *cRole) Update(ctx context.Context, req *v1.RoleUpdateReq) (res *v1.RoleUpdateRes, err error) {
	err = service.Role().Update(ctx, req.Id, model.RoleCreateInput{
		Name: req.Name,
		Slug: req.Slug,
	}, req.Menus)
	return
}

func (r *cRole) Delete(ctx context.Context, req *v1.RoleDeleteReq) (res *v1.Res, err error) {
	err = service.Role().Delete(ctx, req.Id)
	return
}
